<template>
  <div id="register" v-title data-title="注册 - 简易论坛" class="contain">

    <div class="me-login-box me-login-box-radius">
      <h1>简易论坛 注册</h1>

      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="username">
          <el-input placeholder="用户名" v-model="userForm.username"></el-input>
        </el-form-item>

        <el-form-item prop="nickname">
          <el-input placeholder="昵称" v-model="userForm.nickname"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="密码" type="password" v-model="userForm.password"></el-input>
        </el-form-item>

        <el-form-item prop="comfirmPassword">
          <el-input v-model="userForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
        </el-form-item>

        <el-form-item prop="email">
          <el-input v-model="userForm.email" placeholder="邮箱"></el-input>
        </el-form-item>

        <el-form-item prop="mobile">
          <el-input v-model="userForm.mobile" placeholder="手机号"></el-input>
        </el-form-item>

        <el-form-item size="small" class="me-login-button">
          <el-button type="primary" @click.native.prevent="register('userForm')">注册</el-button>
        </el-form-item>
      </el-form>
  

    </div>
  </div>
</template>

<script>
  import { register } from '@/api/login'
  import { isEmail , isMobile } from '@/utils/validate'

  export default {
    name: 'Register',
    data() {
      var validatePassword = (rule, value, callback) => {
        if ( !/\S/.test(value) ) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if ( !/\S/.test(value) ) {
          callback(new Error('确认密码不能为空'))
        } else if (this.userForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        userForm: {
          username: '',
          nickname: '',
          password: '' ,
          comfirmPassword: '' ,
          email: '' ,
          mobile: '' ,
          status: 0 ,
        } ,
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { max: 10, message: '不能大于10个字符', trigger: 'blur' }
          ] ,
          nickname: [
            { required: true, message: '请输入昵称', trigger: 'blur' },
            { max: 10, message: '不能大于10个字符', trigger: 'blur' }
          ] ,
          password: [
            { max: 20, message: '不能大于20个字符', trigger: 'blur' } ,
            { validator: validatePassword, trigger: 'blur' } ,
          ] ,
          comfirmPassword: [
            { max: 20, message: '不能大于20个字符', trigger: 'blur' } ,
            { validator: validateComfirmPassword, trigger: 'blur' } ,
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' } ,
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' } ,
          ] ,
        } ,
      }
    },
    methods: {
      register(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

            this.$store.dispatch('registerAction', this.userForm)
            .then(() => {
              this.$message({
                message: '注册成功 快写文章吧' , 
                type: 'success' ,
                showClose: true
              });
              // 路由回首页
              this.$router.push({path: '/'})
            }).catch((error) => {
              if (error !== 'error') {
                this.$message({
                  message: error, 
                  type: 'error', 
                  showClose: true
                });
              }
            })
          } else {
            return false;
          }
        });
      } ,
    } ,
  }
</script>

<style scoped>
  #login {
    min-width: 100%;
    min-height: 100%;
  }

  .me-video-player {
    background-color: transparent;
    width: 100%;
    height: 100%;
    object-fit: fill;
    display: block;
    position: absolute;
    left: 0;
    z-index: 0;
    top: 0;
  }

  .me-login-box {
    position: absolute;
    width: 300px;
    height: 450px;
    background-color: white;
    margin-left: -180px;
    left: 50%;
    padding: 30px;
  }

  .me-login-box-radius {
    border-radius: 10px;
    box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
  }

  .me-login-box h1 {
    text-align: center;
    font-size: 24px;
    margin-bottom: 20px;
    vertical-align: middle;
  }

  .me-login-design {
    text-align: center;
    font-family: 'Open Sans', sans-serif;
    font-size: 18px;
  }

  .me-login-design-color {
    color: #5FB878 !important;
  }

  .me-login-button {
    text-align: center;
  }

  .me-login-button button {
    width: 100%;
  }

  .contain {
    padding-bottom: 50px;
    padding-top: 50px;
  }

</style>
